<?php
    require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
    require $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/doctype.php';

    function addKoma($query, $count)
    {
        if ($count > 0)
        {
            $query .= ", ";
        }
    }
?>

<head>
    <title> Edit your profile </title>
    <link rel="StyleSheet" href="../css/indah.css" type="text/css" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="update_validation.js"></script>
    <script type="text/javascript" src="dateselector.js"></script>
</head>

<body>
    <?php
        $userId = 14;//$_SESSION['user'];

        //Ambil dulu datanya dari database
        $result = mysql_query("SELECT * FROM users WHERE id = $userId");
        $result = mysql_fetch_array($result);

    ?>
    <div class="background">
        
<!--        <h1>DeviantPIC</h1>
        <h2>Easy way to share your photo</h2>-->

    <div id='breakline'></div><br/><br/><br/><br/><br/><br/><br/><br/>

    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" onsubmit="return check(this)">

        <!-- Username input -->
        <label for="input_username">Username </label>
        <input type="text"
                   id="input_username"
                   name="username"
                   value="<? echo $result['username'] ?>"
        /> <span id="result_username"></span> <br />

        <!-- Password input -->
        <label for="input_password">Password </label>
        <input type="password"
                   id="input_password"
                   name="password"
                   value="<? echo $result['password'] ?>"
        /> <span id="result_password"></span> <br />
        <!-- E-Mail input -->
        <label for="input_email">E-Mail </label>
        <input type="text"
                   id="input_email"
                   name="email"
                   value="<? echo $result['email'] ?>"
        /> <span id="result_email"></span> <br />

        <!-- Name input -->
        <label for="input_name">Real name </label>
        <input type="text"
                   id="input_name"
                   name="name"
                   value="<? echo $result['username'] ?>"
        /> <span id="result_name"></span> <br />

        <!-- Date input -->
        <label for="input_birth">Birth date </label>
        <input type="text"
                   id="calDate"
                   name="birth"
                   value="<? echo $result['tanggal lahir'] ?>"
        />
        <input type="button" value="date selector" onclick="document.getElementById('cal').innerHTML = dispCal(0,0);"/><br/>
        <div id="cal" style="position:relative; left:40px;top:15px;"></div>
        <span id="result_birth"></span> <br />

        <!-- Gender -->
        <label for="input_gender">Gender </label>
        <select name="gender" id="input_gender">
                <option value="Laki-laki">Male</option>
                <option value="Perempuan">Female</option>
        </select> <br />

        <!-- Location input -->
        <label for="input_location">Location </label>
        <input type="text"
                   id="input_location"
                   name="location"
                   value="<? echo $result['lokasi'] ?>"
        /> <span id="result_location"></span> <br />

        <!--Wall Privilege-->
        <label for="wall_privilege"> Wall privilege </label>
        <select name="wall" id="wall_privilege">
            <option value="0">0</option>
            <option value="1">1</option>
        </select><br/>

        <!-- Profile Picture -->
        <label for="profilePicture"> Profile picture </label>
        <?php
                $result = mysql_query("SELECT photo FROM photos WHERE user_id = $userId");
                $option = "<select name =\"profpic\" id=\"profpic\">";
                while ($rows = mysql_fetch_array($result))
                {
                    $option .= "<option value=\"".$rows['photo']."\">".$rows['photo']."</option>";
                }
                $option .= "</select>";

                echo $option;
	?>
        <!-- Button -->
        <input type="submit" name="submit" value="Update" />
        
    </form>
    <?php
        if ($_POST['submit'])
        {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $email = $_POST['email'];
            $birthDate = $_POST['birth'];
            $gender = $_POST['gender'];
            $location = $_POST['location'];
            $wallPriv = $_POST['wall'];
            $profpic = mysql_fetch_array(mysql_query("SELECT id FROM photos WHERE photo='".$_POST['profpic']."'"));
            $profpic = $profpic['id'];
            
            $queryUpdate = "UPDATE `deviant`.`users` SET ";
            $count = 0;

            if ($username)
            {
                $queryUpdate .= "username = '$username' ";
                ++$count;
            }
            if ($password) 
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "password = '$password' ";
                ++$count;
            }
             if ($email)
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "email = '$email' ";
                ++$count;
            }
            if ($birthDate)
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "tanggal_lahir = '$birthDate' ";
                ++$count;
            }
            if ($gender)
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "jenis_kelamin = '$gender' ";
                ++$count;
            }
            if ($location)
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "lokasi = '$location' ";
                ++$count;
            }
            if ($profpic)
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "avatar_id = $profpic ";
                ++$count;
            }
            if ($wallPriv)
            {
                if ($count > 0) $queryUpdate .= ", ";
                $queryUpdate .= "wall_privilege = $wallPriv ";
            }

            $queryUpdate .= "WHERE id = " . $userId;

            echo $queryUpdate;

            echo "<br/>Status update : " . mysql_query($queryUpdate);
        }
    ?>
</div>
</body>
